package com.ruoyi.web.controller.business.goods.factory;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.business.goods.domain.JbProductClassify;
import com.ruoyi.business.goods.service.JbProductClassifyService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.controller.business.goods.vo.JbProductClassifyVO;
import com.ruoyi.web.core.JoController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品商品分类 Controller
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@Controller
public class JbProductClassifyFactory extends JoController
{
    @Autowired
    private JbProductClassifyService service;

    /**
     * 商品类型
     */
    final public static Long COMMODIYTYPE = 1L;
    /**
     * 产品类型
     */
    final public static Long PRODUCTTYPE = 0L;

    /**
     * 查询树
     */
    public JSONArray listAll(Long type)
    {
        LambdaQueryWrapper<JbProductClassify> select = new LambdaQueryWrapper<>();
        select.eq(JbProductClassify::getType, type);

        List<JbProductClassify> list = service.list(select);
        return toVO(list, JbProductClassifyVO.class);
    }

    /**
     * 封装成中文字典
     *
     * @param type 类型
     * @return 中文字典
     */
    public Map<String, Long> dict(Long type)
    {
        Map<String, Long> map = new HashMap<>();

        LambdaQueryWrapper<JbProductClassify> select = new LambdaQueryWrapper<>();
        select.eq(JbProductClassify::getType, type);

        List<JbProductClassify> list = service.list(select);
        for (JbProductClassify jbProductClassify : list)
        {
            map.put(jbProductClassify.getName(), jbProductClassify.getId());
        }
        return map;
    }

    /**
     * 新增
     */
    public AjaxResult addOrEdit(JbProductClassify jbProductClassify)
    {
        jbProductClassify.setCreateDeptId(getDeptId().toString());
        return toAjax(service.saveOrUpdate(jbProductClassify));
    }

    /**
     * 删除
     */
    public AjaxResult remove(Long[] ids)
    {
        return toAjax(service.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 获取
     */
    public AjaxResult getInfo(Long id)
    {
        return success(service.getById(id));
    }

}
